Instructions

Answer the following questions and complete the exercises in RMarkdown. Please embed all of your code and push your final work to your repository. Your final lab report should be organized, clean, and run free from errors. Remember, you must remove the # for the included code chunks to run. Be sure to add your name to the author header above. For any included plots, make sure they are clearly labeled. You are free to use any plot type that you feel best communicates the results of your analysis.

Make sure to use the formatting conventions of RMarkdown to make your report neat and clean!

Load the libraries

library(tidyverse)
library(janitor)
library(ggmap)

Load the Data

We will use two separate data sets for this homework.

  1. The first data set represent sightings of grizzly bears (Ursos arctos) in Alaska.

  2. The second data set is from Brandell, Ellen E (2021), Serological dataset and R code for: Patterns and processes of pathogen exposure in gray wolves across North America, Dryad, Dataset.

register_stadiamaps("311578bf-a7b3-4a46-955f-50b1b0fe5edc", write = FALSE)
  1. Load the grizzly data and evaluate its structure.
grizzly <- read_csv("data/bear-sightings.csv")%>% clean_names()
## Rows: 494 Columns: 3
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## dbl (3): bear.id, longitude, latitude
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
summary(grizzly)
##     bear_id       longitude         latitude    
##  Min.   :   7   Min.   :-166.2   Min.   :55.02  
##  1st Qu.:2569   1st Qu.:-154.2   1st Qu.:58.13  
##  Median :4822   Median :-151.0   Median :60.97  
##  Mean   :4935   Mean   :-149.1   Mean   :61.41  
##  3rd Qu.:7387   3rd Qu.:-145.6   3rd Qu.:64.13  
##  Max.   :9996   Max.   :-131.3   Max.   :70.37
  1. Use the range of the latitude and longitude to build an appropriate bounding box for your map.
grizzly %>% 
  select(latitude, longitude) %>% 
  summary()
##     latitude       longitude     
##  Min.   :55.02   Min.   :-166.2  
##  1st Qu.:58.13   1st Qu.:-154.2  
##  Median :60.97   Median :-151.0  
##  Mean   :61.41   Mean   :-149.1  
##  3rd Qu.:64.13   3rd Qu.:-145.6  
##  Max.   :70.37   Max.   :-131.3
longitude <- c(-166.2, -131.3)
latitude <- c(55.02, 70.37)
gri_bbox <- make_bbox(longitude, latitude, f = 0.03)
  1. Load a map from stamen in a terrain style projection and display the map.
sta <- get_stadiamap(gri_bbox,maptype = "stamen_terrain", zoom=7)
## ℹ © Stadia Maps © Stamen Design © OpenMapTiles © OpenStreetMap contributors.
## ℹ 196 tiles needed, this may take a while (try a smaller zoom?)
ggmap(sta)

  1. Build a final map that overlays the recorded observations of grizzly bears in Alaska.
ggmap(sta)+
  geom_point(data=grizzly, aes(x=longitude, y=latitude), size=0.8)+
  labs(x="Longitude", y="Latitude", title="Observations of grizzly bears in Alaska")

Let’s switch to the wolves data. Brandell, Ellen E (2021), Serological dataset and R code for: Patterns and processes of pathogen exposure in gray wolves across North America, Dryad, Dataset.

  1. Load the data and evaluate its structure.
wolves <- read.csv("data/wolves_data/wolves_dataset.csv") %>% clean_names()
summary(wolves)
##      pop                 year        age_cat              sex           
##  Length:1986        Min.   :1992   Length:1986        Length:1986       
##  Class :character   1st Qu.:2006   Class :character   Class :character  
##  Mode  :character   Median :2011   Mode  :character   Mode  :character  
##                     Mean   :2010                                        
##                     3rd Qu.:2016                                        
##                     Max.   :2019                                        
##                                                                         
##     color                lat             long            habitat       
##  Length:1986        Min.   :33.89   Min.   :-157.84   Min.   :  254.1  
##  Class :character   1st Qu.:44.60   1st Qu.:-123.73   1st Qu.:10375.2  
##  Mode  :character   Median :46.83   Median :-110.99   Median :11211.3  
##                     Mean   :50.43   Mean   :-116.86   Mean   :12797.4  
##                     3rd Qu.:57.89   3rd Qu.:-110.55   3rd Qu.:11860.8  
##                     Max.   :80.50   Max.   : -82.42   Max.   :34676.6  
##                                                                        
##      human          pop_density      pack_size    standard_habitat  
##  Min.   :   0.02   Min.   : 3.74   Min.   :3.55   Min.   :-1.63390  
##  1st Qu.:  80.60   1st Qu.: 7.40   1st Qu.:5.62   1st Qu.:-0.30620  
##  Median :2787.67   Median :11.63   Median :6.37   Median :-0.19650  
##  Mean   :2335.38   Mean   :14.91   Mean   :6.47   Mean   : 0.01158  
##  3rd Qu.:3973.47   3rd Qu.:25.32   3rd Qu.:8.25   3rd Qu.:-0.11130  
##  Max.   :6228.64   Max.   :33.96   Max.   :9.56   Max.   : 2.88180  
##                                                                     
##  standard_human     standard_pop      standard_packsize standard_latitude  
##  Min.   :-0.9834   Min.   :-1.13460   Min.   :-1.7585   Min.   :-1.805900  
##  1st Qu.:-0.9444   1st Qu.:-0.74630   1st Qu.:-0.5418   1st Qu.:-0.636900  
##  Median : 0.3648   Median :-0.29760   Median :-0.1009   Median :-0.392600  
##  Mean   : 0.1461   Mean   : 0.05084   Mean   :-0.0422   Mean   :-0.000006  
##  3rd Qu.: 0.9383   3rd Qu.: 1.15480   3rd Qu.: 1.0041   3rd Qu.: 0.814300  
##  Max.   : 2.0290   Max.   : 2.07150   Max.   : 1.7742   Max.   : 3.281900  
##                                                                            
##  standard_longitude    cav_binary       cdv_binary       cpv_binary    
##  Min.   :-2.144100   Min.   :0.0000   Min.   :0.0000   Min.   :0.0000  
##  1st Qu.:-0.359500   1st Qu.:1.0000   1st Qu.:0.0000   1st Qu.:1.0000  
##  Median : 0.306900   Median :1.0000   Median :0.0000   Median :1.0000  
##  Mean   :-0.000005   Mean   :0.8529   Mean   :0.2219   Mean   :0.7943  
##  3rd Qu.: 0.330200   3rd Qu.:1.0000   3rd Qu.:0.0000   3rd Qu.:1.0000  
##  Max.   : 1.801500   Max.   :1.0000   Max.   :1.0000   Max.   :1.0000  
##                      NA's   :321      NA's   :21       NA's   :7       
##    chv_binary       neo_binary      toxo_binary    
##  Min.   :0.0000   Min.   :0.0000   Min.   :0.0000  
##  1st Qu.:1.0000   1st Qu.:0.0000   1st Qu.:0.0000  
##  Median :1.0000   Median :0.0000   Median :0.0000  
##  Mean   :0.8018   Mean   :0.2804   Mean   :0.4832  
##  3rd Qu.:1.0000   3rd Qu.:1.0000   3rd Qu.:1.0000  
##  Max.   :1.0000   Max.   :1.0000   Max.   :1.0000  
##  NA's   :548      NA's   :538      NA's   :827
  1. How many distinct wolf populations are included in this study? Make a new object that restricts the data to the wolf populations in the lower 48 US states.
n_distinct(wolves$pop)
## [1] 17
table(wolves$pop)
## 
##  AK.PEN BAN.JAS      BC  DENALI   ELLES    GTNP  INT.AK MEXICAN      MI      MT 
##     100      96     145     154      11      60      35     181     102     351 
##   N.NWT     ONT   SE.AK     SNF  SS.NWT     YNP    YUCH 
##      67      60      10      92      34     383     105
new_wolves <- filter(wolves, pop %in% c("MT", "YNP", "GTNP", "MI"))
summary(new_wolves)
##      pop                 year        age_cat              sex           
##  Length:896         Min.   :1997   Length:896         Length:896        
##  Class :character   1st Qu.:2006   Class :character   Class :character  
##  Mode  :character   Median :2010   Mode  :character   Mode  :character  
##                     Mean   :2009                                        
##                     3rd Qu.:2014                                        
##                     Max.   :2019                                        
##                                                                         
##     color                lat             long            habitat     
##  Length:896         Min.   :43.82   Min.   :-110.99   Min.   : 9511  
##  Class :character   1st Qu.:44.60   1st Qu.:-110.99   1st Qu.:11166  
##  Mode  :character   Median :46.15   Median :-110.55   Median :11166  
##                     Mean   :45.60   Mean   :-108.03   Mean   :10944  
##                     3rd Qu.:46.83   3rd Qu.:-110.55   3rd Qu.:11211  
##                     Max.   :46.83   Max.   : -86.82   Max.   :11211  
##                                                                      
##      human       pop_density      pack_size     standard_habitat 
##  Min.   :3240   Min.   :11.63   Min.   :5.620   Min.   :-0.4196  
##  1st Qu.:3240   1st Qu.:11.63   1st Qu.:5.620   1st Qu.:-0.2024  
##  Median :3973   Median :23.03   Median :7.120   Median :-0.2024  
##  Mean   :3939   Mean   :21.82   Mean   :7.081   Mean   :-0.2316  
##  3rd Qu.:3973   3rd Qu.:28.93   3rd Qu.:8.250   3rd Qu.:-0.1965  
##  Max.   :6229   Max.   :33.96   Max.   :8.250   Max.   :-0.1965  
##                                                                  
##  standard_human    standard_pop     standard_packsize standard_latitude
##  Min.   :0.5834   Min.   :-0.2976   Min.   :-0.5418   Min.   :-0.7219  
##  1st Qu.:0.5834   1st Qu.:-0.2976   1st Qu.:-0.5418   1st Qu.:-0.6369  
##  Median :0.9383   Median : 0.9119   Median : 0.3399   Median :-0.4677  
##  Mean   :0.9218   Mean   : 0.7833   Mean   : 0.3170   Mean   :-0.5276  
##  3rd Qu.:0.9383   3rd Qu.: 1.5378   3rd Qu.: 1.0041   3rd Qu.:-0.3926  
##  Max.   :2.0290   Max.   : 2.0715   Max.   : 1.0041   Max.   :-0.3926  
##                                                                        
##  standard_longitude   cav_binary       cdv_binary       cpv_binary    
##  Min.   :0.3069     Min.   :0.0000   Min.   :0.0000   Min.   :0.0000  
##  1st Qu.:0.3069     1st Qu.:1.0000   1st Qu.:0.0000   1st Qu.:1.0000  
##  Median :0.3302     Median :1.0000   Median :0.0000   Median :1.0000  
##  Mean   :0.4618     Mean   :0.8326   Mean   :0.2905   Mean   :0.8881  
##  3rd Qu.:0.3302     3rd Qu.:1.0000   3rd Qu.:1.0000   3rd Qu.:1.0000  
##  Max.   :1.5716     Max.   :1.0000   Max.   :1.0000   Max.   :1.0000  
##                     NA's   :6        NA's   :1        NA's   :2       
##    chv_binary       neo_binary      toxo_binary    
##  Min.   :0.0000   Min.   :0.0000   Min.   :0.0000  
##  1st Qu.:1.0000   1st Qu.:0.0000   1st Qu.:0.0000  
##  Median :1.0000   Median :0.0000   Median :0.0000  
##  Mean   :0.7854   Mean   :0.3692   Mean   :0.4569  
##  3rd Qu.:1.0000   3rd Qu.:1.0000   3rd Qu.:1.0000  
##  Max.   :1.0000   Max.   :1.0000   Max.   :1.0000  
##  NA's   :183      NA's   :189      NA's   :478
  1. Use the range of the latitude and longitude to build an appropriate bounding box for your map.
new_wolves %>% 
  select(long, lat) %>% 
  summary()
##       long              lat       
##  Min.   :-110.99   Min.   :43.82  
##  1st Qu.:-110.99   1st Qu.:44.60  
##  Median :-110.55   Median :46.15  
##  Mean   :-108.03   Mean   :45.60  
##  3rd Qu.:-110.55   3rd Qu.:46.83  
##  Max.   : -86.82   Max.   :46.83
long <-  c(-110.99, -86.82)
lat <- c(43.82, 46.83)
wolf <- make_bbox(long, lat, f=0.05)
  1. Load a map from stamen in a terrain-lines projection and display the map.
new_map <- get_stadiamap(wolf, maptype = "stamen_terrain_lines", zoom = 5)
## ℹ © Stadia Maps © Stamen Design © OpenMapTiles © OpenStreetMap contributors.
ggmap(new_map)

  1. Build a final map that overlays the recorded observations of wolves in the lower 48 states.
ggmap(new_map)+
  geom_point(data=new_wolves, aes(x=long, y=lat), size=0.8)+
  labs(x="Longitude", y="Latitude", title="Observations of wolves in lower 48 states")

  1. Use the map from #9 above, but add some aesthetics. Try to fill and color by population.
ggmap(new_map)+
  geom_point(data=new_wolves, aes(x=long, y=lat, color = pop), size=0.8)+
  labs(x="Longitude", y="Latitude", title="Observations of wolves in lower 48 states")

Push your final code to GitHub!

Please be sure that you check the keep md file in the knit preferences.